rm(list=ls(all=TRUE))

path <- "" #here you need to adjust the path macro to the place on your computer
setwd(path)

library(haven)
library(dplyr)
library(ggplot2)
library(Hmisc)
library(ggrepel)
library(xlsx)
library(cowplot)
require(lubridate)
library(janitor)
library(purrr)
library(tidyr)

prot_dat <- read_dta(paste0(path, "\\long_chapter_sep.dta"))
prot_dat$iso2code <- as_factor(prot_dat$iso2code)
class(prot_dat$newdate)

##################################
###### Appendix E table 1 ########
##################################

prot_dat <- prot_dat %>% 
  select(iso2code, party_id, newdate, big_event, economic, 
         NSM, ANSM, political, unions, otherorg,
         petition, strike, confrontation, violence, demo, gov,
         vote_share, seat_share, aglpopw_event,
         aft_elect_prt, bef_elect_prt) %>% 
  mutate(year_month=format(newdate, "%Y-%m")) %>% 
  filter(!is.na(party_id)) %>% 
  mutate(iso2code = as.character(iso2code),
         party_id = as.numeric(party_id)) %>% 
  arrange(newdate, party_id) %>% 
  select(newdate, party_id, everything())

dat <- read_dta(paste0(path, "\\party_governments.dta"))

dat <- dat %>% 
  select(party_id, iso2code, family2, pm_counter, mass_org_scale, 
         state_market, liberty_authority, left_right) %>% 
  mutate(past_pm = ifelse(pm_counter>=1, 1,
                          ifelse(is.na(pm_counter) | pm_counter<1, 0, 0)),
         mass_party=ifelse(mass_org_scale>quantile(mass_org_scale, na.rm=T)[3], 1, 
                           ifelse(mass_org_scale<=quantile(mass_org_scale, na.rm=T)[3] |
                                    is.na(mass_org_scale),0,0))) %>%
  mutate(past_pm=ifelse(is.na(past_pm), 0, past_pm),
         mass_party=ifelse(is.na(mass_party),0, mass_party)) %>% 
  select(-pm_counter, -mass_org_scale) %>%
  group_by(iso2code, party_id) %>% 
  mutate(past_pm=max(past_pm),
         mass_party=max(mass_party),
         left_right = mean(left_right, na.rm = TRUE),
         state_market=mean(state_market, na.rm = TRUE),
         liberty_authority=mean(liberty_authority, na.rm = TRUE)) %>% 
  ungroup(.) %>% 
  mutate(iso2code = as.character(iso2code),
         party_id = as.numeric(party_id)) %>% 
  select(-iso2code) %>% 
  unique(.)

dat <- left_join(prot_dat, dat, by="party_id")
dat <- subset(dat, party_id>0)
dat <- dat[order(dat$newdate, dat$party_id),]
dat <- dat %>% select(newdate, party_id, everything())

colnames(dat)
dim(dat)

rm(prot_dat)

# Event level characteristics

table(dat$big_event)
table(dat$economic)
table(dat$NSM)
table(dat$ANSM)
table(dat$political)
table(dat$unions)
table(dat$otherorg)
table(dat$petition)
table(dat$strike)
table(dat$confrontation)
table(dat$violence)
table(dat$demo)

# party level characteristics
colnames(dat)
table(dat$family2)
dat$left_prt[dat$left_right<5] <- 1
dat$left_prt[dat$left_right>=5] <- 0

dat$eleft_prt[dat$state_market<5] <- 1
dat$eleft_prt[dat$state_market>=5] <- 0

dat$cleft_prt[dat$liberty_authority<5] <- 1
dat$cleft_prt[dat$liberty_authority>=5] <- 0

table(dat$past_pm)
table(dat$mass_party)
table(dat$gov)

dat$marginal[dat$seat_share==0] <- 1
dat$marginal[!is.na(dat$seat_share) & is.na(dat$marginal)] <- 0

dat$small[dat$seat_share>0 & dat$seat_share<10] <- 1
dat$small[!is.na(dat$seat_share) & is.na(dat$small)] <- 0

dat$mid_size[dat$seat_share>=10 & dat$seat_share<25] <- 1
dat$mid_size[!is.na(dat$seat_share) & is.na(dat$mid_size)] <- 0

dat$large[dat$seat_share>=25] <- 1
dat$large[!is.na(dat$seat_share) & is.na(dat$large)] <- 0

table(dat$small)
table(dat$marginal)
table(dat$mid_size)
table(dat$large)

table(dat$seat_share[dat$marginal==1])
table(dat$seat_share[dat$small==1])
table(dat$seat_share[dat$mid_size==1])
table(dat$seat_share[dat$large==1])

table(dat$vote_share[dat$marginal==1])
table(dat$vote_share[dat$small==1])
table(dat$vote_share[dat$mid_size==1])
table(dat$vote_share[dat$large==1])

dat$mainstream[dat$family2==2 | dat$family2==4 | dat$family2==6 | dat$family2==8] <- 1
dat$mainstream[is.na(dat$mainstream) & !is.na(dat$family2)] <- 0
dat$mainstream <- as.factor(dat$mainstream)

dat$cultural[dat$NSM==1 | dat$ANSM==1] <- 1
dat$cultural[is.na(dat$cultural)] <- 0
dat$cultural <- as.factor(dat$cultural)
table(dat$cultural)

dat$coorg[dat$unions==1 | dat$otherorg==1] <- 1
dat$coorg[is.na(dat$coorg)] <- 0

dat$demo_petition[dat$petition==1 | dat$strike==1] <- 1
dat$demo_petition[is.na(dat$demo_petition)] <- 0

dat$conf_viol[dat$confrontation==1 | dat$violence==1] <- 1
dat$conf_viol[is.na(dat$conf_viol)] <- 0

diff.cntry <- c("BE" ,"DK" ,"FI" ,"FR" ,"GR" ,"IE" ,"IT" ,"LU" ,"MT" ,"NL" ,"NO" ,"PT" ,"RO" ,"SI" ,"UK")
nodiff.cntry <- c("AT" ,"BG" ,"CY" ,"CZ" ,"EE" ,"DE" ,"HU" ,"IS" ,"LV" ,"LT" ,"PL" ,"SK" ,"ES" ,"SE" ,"CH")

dat$diff[dat$iso2code %in% diff.cntry] <- "HiDi"
dat$diff[dat$iso2code %in% nodiff.cntry] <- "LoDi"

dat$diff <- relevel(as.factor(dat$diff), "LoDi")

table(dat$petition)

dat$opp_party <- ifelse(dat$gov==0, 1, 0)
table(dat$opp_party)

table(dat$aft_elect_prt) 
table(dat$bef_elect_prt)

party_charac <- c("past_pm", "mass_party", "mainstream", "opp_party",
                  "left_prt", "eleft_prt", "cleft_prt", "marginal",
                  "small", "mid_size", "large")

event_charac <- c("big_event", "economic", "cultural",
                  "coorg", "demo", "conf_viol",
                  "aft_elect_prt", "bef_elect_prt")

dat <- subset(dat, select=c(colnames(dat) %in% party_charac |
                       colnames(dat) %in% event_charac |
                       colnames(dat) %in% c("diff", "aglpopw_event")))
colnames(dat)


dat <- dat %>% mutate_at(vars(event_charac, party_charac),
                  function(x, na.rm=T) (ifelse(x==1, .$aglpopw_event, 0)))


# here start with LoDi

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(big_event>0, 1, 0)) %>% 
  select(big_event, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(big_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(big_event=ifelse(event_group==0, aglpopw_event, big_event)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(big_event)) %>% 
  mutate(event_group = ifelse(big_event>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
             function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$big_event - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -big_event) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -big_event)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "big_event"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- successes

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(economic>0, 1, 0)) %>% 
  select(economic, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(economic), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(economic=ifelse(event_group==0, aglpopw_event, economic)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(economic)) %>% 
  mutate(event_group = ifelse(economic>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$economic - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -economic) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -economic)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "economic"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(cultural>0, 1, 0)) %>% 
  select(cultural, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(cultural), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(cultural=ifelse(event_group==0, aglpopw_event, cultural)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(cultural)) %>% 
  mutate(event_group = ifelse(cultural>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$cultural - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -cultural) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -cultural)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "cultural"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(coorg>0, 1, 0)) %>% 
  select(coorg, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(coorg), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(coorg=ifelse(event_group==0, aglpopw_event, coorg)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(coorg)) %>% 
  mutate(event_group = ifelse(coorg>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$coorg - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -coorg) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -coorg)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "coorg"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(demo>0, 1, 0)) %>% 
  select(demo, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(demo), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(demo=ifelse(event_group==0, aglpopw_event, demo)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(demo)) %>% 
  mutate(event_group = ifelse(demo>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$demo - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -demo) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -demo)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "demo"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(conf_viol>0, 1, 0)) %>% 
  select(conf_viol, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(conf_viol), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(conf_viol=ifelse(event_group==0, aglpopw_event, conf_viol)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(conf_viol)) %>% 
  mutate(event_group = ifelse(conf_viol>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$conf_viol - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -conf_viol) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -conf_viol)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "conf_viol"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(aft_elect_prt>0, 1, 0)) %>% 
  select(aft_elect_prt, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(aft_elect_prt), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(aft_elect_prt=ifelse(event_group==0, aglpopw_event, aft_elect_prt)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(aft_elect_prt)) %>% 
  mutate(event_group = ifelse(aft_elect_prt>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$aft_elect_prt - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -aft_elect_prt) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -aft_elect_prt)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "aft_elect_prt"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="LoDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(bef_elect_prt>0, 1, 0)) %>% 
  select(bef_elect_prt, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(bef_elect_prt), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(bef_elect_prt=ifelse(event_group==0, aglpopw_event, bef_elect_prt)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(bef_elect_prt)) %>% 
  mutate(event_group = ifelse(bef_elect_prt>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$bef_elect_prt - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -bef_elect_prt) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -bef_elect_prt)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "bef_elect_prt"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

LoDi <- rbind(first, successes)

# here start with HiDi

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(big_event>0, 1, 0)) %>% 
  select(big_event, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(big_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(big_event=ifelse(event_group==0, aglpopw_event, big_event)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(big_event)) %>% 
  mutate(event_group = ifelse(big_event>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$big_event - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -big_event) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -big_event)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "big_event"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- successes

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(economic>0, 1, 0)) %>% 
  select(economic, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(economic), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(economic=ifelse(event_group==0, aglpopw_event, economic)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(economic)) %>% 
  mutate(event_group = ifelse(economic>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$economic - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -economic) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -economic)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "economic"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(cultural>0, 1, 0)) %>% 
  select(cultural, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(cultural), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(cultural=ifelse(event_group==0, aglpopw_event, cultural)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(cultural)) %>% 
  mutate(event_group = ifelse(cultural>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$cultural - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -cultural) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -cultural)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "cultural"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(coorg>0, 1, 0)) %>% 
  select(coorg, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(coorg), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(coorg=ifelse(event_group==0, aglpopw_event, coorg)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(coorg)) %>% 
  mutate(event_group = ifelse(coorg>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$coorg - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -coorg) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -coorg)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "coorg"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(demo>0, 1, 0)) %>% 
  select(demo, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(demo), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(demo=ifelse(event_group==0, aglpopw_event, demo)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(demo)) %>% 
  mutate(event_group = ifelse(demo>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$demo - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -demo) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -demo)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "demo"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(conf_viol>0, 1, 0)) %>% 
  select(conf_viol, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(conf_viol), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(conf_viol=ifelse(event_group==0, aglpopw_event, conf_viol)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(conf_viol)) %>% 
  mutate(event_group = ifelse(conf_viol>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$conf_viol - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -conf_viol) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -conf_viol)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "conf_viol"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(aft_elect_prt>0, 1, 0)) %>% 
  select(aft_elect_prt, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(aft_elect_prt), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(aft_elect_prt=ifelse(event_group==0, aglpopw_event, aft_elect_prt)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(aft_elect_prt)) %>% 
  mutate(event_group = ifelse(aft_elect_prt>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$aft_elect_prt - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -aft_elect_prt) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -aft_elect_prt)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "aft_elect_prt"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

first <- rbind(first, successes)

by_diff <- subset(dat, diff=="HiDi")

totals <- by_diff %>% 
  mutate(event_group = ifelse(bef_elect_prt>0, 1, 0)) %>% 
  select(bef_elect_prt, event_group, aglpopw_event) %>%
  group_by(event_group) %>% 
  mutate_at(vars(bef_elect_prt), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate_at(vars(aglpopw_event), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  mutate(bef_elect_prt=ifelse(event_group==0, aglpopw_event, bef_elect_prt)) %>% 
  select(-aglpopw_event) %>% 
  unique(.)

totals_by_event <- by_diff %>% 
  filter(!is.na(bef_elect_prt)) %>% 
  mutate(event_group = ifelse(bef_elect_prt>0, 1, 0)) %>% 
  group_by(event_group) %>%
  select(party_charac, "event_group") %>% 
  mutate_at(vars(party_charac), function(x, na.rm=T) sum(x, na.rm=T)) %>% 
  unique(.)

totals_by_event <- left_join(totals_by_event, totals, by="event_group")

totals <- totals_by_event %>% 
  select(-event_group) %>% 
  ungroup(.) %>% 
  summarise_all(funs(sum)) %>% 
  unique() %>% 
  mutate(event_group=3)

totals_by_event <- bind_rows(totals_by_event, totals)
# totals_by_event <- reshape2::melt(totals_by_event, id.vars="event_group")

# reshape(totals_by_event, timevar = "event_group", direction="wide")

term1 <- totals_by_event %>% 
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1) %>% 
  mutate(type=1)

term2 <- totals_by_event %>%
  ungroup(.) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) totals_by_event$bef_elect_prt - x) %>% 
  mutate_at(vars(party_charac),
            function(x, na.rm=T) x/dplyr::lead(x, order_by = totals_by_event$event_group, n=1)*100) %>% 
  filter(event_group==1)  %>% 
  mutate(type=2)

shares <- bind_rows(term1, term2)
shares <- shares %>% 
  select(-event_group, -bef_elect_prt) %>% 
  reshape2::melt(., id.vars="type") %>% 
  group_by(variable) %>% 
  mutate(delta=value-dplyr::lead(value, order_by = type, n=1))

successes <- totals_by_event %>% 
  ungroup(.) %>% 
  filter(event_group!=0) %>% 
  mutate(type = ifelse(event_group==1, "success_rate", "total"))

term2$type <- as.character("prop")

successes <- bind_rows(successes, term2)

successes <- successes %>% 
  select(-event_group, -bef_elect_prt)

successes <- as.data.frame(t(successes))
successes$names <- rownames(successes)
colnames(successes) <- c("success_rate", "total", "prop", "type")
successes <- subset(successes, type!="type")
successes$success_rate <- as.numeric(as.character(successes$success_rate))
successes$total <- as.numeric(as.character(successes$total))
successes$prop <- as.numeric(as.character(successes$prop))/100

prop_test_list <- pmap(successes, function(success_rate, total, prop, ...)
  prop.test(x = success_rate, n = total, 
            p=prop, 
            alternative = 'two.sided', conf.level = 0.95))

p_values <- map_dfr(prop_test_list, ~ data.frame(p = .x$p.value, estimate = .x$estimate, confint_min = .x$conf.int[1], confint_max = .x$conf.int[2]))

successes <- cbind(successes, p_values)
successes$prop <- successes$prop * 100
successes$estimate <- successes$estimate * 100
successes$delta <- successes$estimate-successes$prop
successes$significant <- ifelse(successes$p <= 0.05, 1, 0) 
successes$event <- "bef_elect_prt"
successes <- subset(successes, select = c("type", "delta", "estimate", "prop", "significant", "event"))

HiDi <- rbind(first, successes)


# some manipulation

LoDi$data <- "LoDi"
HiDi$data <- "HiDi"

results <- rbind(LoDi, HiDi)

rm(LoDi, HiDi)

table(results$significant)

head(results)

results2 <- results %>% 
  mutate_at(vars(estimate, delta, prop),
            function(x, na.rm=T)(round(x, digits=1))) %>% 
  mutate(event= paste0(data, sep="_", event)) %>% 
  select(-data)

head(results2)

results2 <- results2 %>% 
  gather(key, value,-c(event, type)) %>% 
  tidyr::spread(type, value) 

library(xlsx)
write.xlsx(results,
           file="\\appendix_table2.xlsx")
